# -*- coding: utf-8 -*-
# @Time    : 2018-05-21 20:47
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : CreditOperationOverdueCnt.py
# @Software: PyCharm

'''eof
name:贷款及其他账户逾期总数
code:CreditOperationOverdueCnt
tableName:
columnName:
groups:个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', None, 'null', '/', 'Null']


def FormatData(table_name):
    try:
        icr = json.loads(IC[0]["icr"])[table_name]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]


def ConvertDataType(dataValue, dataType):
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) if float(dataValue) >= 0 else None
        except:
            returnData = None
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    return returnData


def CreditOperationOverdueCnt():
    try:
        # 逾期(透支)信息汇总
        icrOverdueSummary = FormatData('ICROverdueSummary')
        if icrOverdueSummary == [{}]:
            return u'缺失值'
        else:
            # 贷款逾期笔数
            count = ConvertDataType(icrOverdueSummary[0].get('Count'), 'float')
            # 贷记卡逾期账户数
            count2 = ConvertDataType(icrOverdueSummary[0].get('Count2'), 'float')
            # 准贷记卡60天以上透支账户数
            count3 = ConvertDataType(icrOverdueSummary[0].get('Count3'), 'float')

            if count in null_type_list and count2 in null_type_list and count3 in null_type_list:
                return u'缺失值'
            return ConvertDataType(count, 'None')+ConvertDataType(count2, 'None')+ConvertDataType(count3, 'None')

    except:
        return u'缺失值'


result = CreditOperationOverdueCnt()
